<template>
  <view class="branch-list-wrap">
    <view class="list">
      <skeleton row="2" :loading="loading">
        <view v-for="item in branchList" :key="item.id">
          <view class="list__li" @tap.stop="handleSelect(item)">
            <view class="list__li--title">
              <view class="list__li--title-name">{{ item.name }}</view>
            </view>
            <view class="list__li--address">
              {{ item.address }}{{ item.contactor_name }}
            </view>
          </view>
        </view>
      </skeleton>
      <skeleton row="2" :loading="loading" />
    </view>

    <view v-if="!branchList.length && !loading">
      <u-empty description="暂无网点~"></u-empty>
    </view>
  </view>
</template>

<script>
import get from 'lodash/get';
import { QUERY_BRANCH } from '@/api';
import uEmpty from '@/basic-components/u-empty/index.vue';
import Skeleton from '@/basic-components/u-skeleton/index.vue';


export default {
  components: {
    uEmpty,
    Skeleton,
  },
  data() {
    return {
      branchList: [],
      loading: true,
    };
  },
  onShow() {
    this.fetchBrachList();
  },
  methods: {
    // 获取一下地址
    async fetchBrachList() {
      this.loading = true;
      let params = {
        regionId: uni.getStorageSync('regionId'),
      };
      const branchRes = await this.$http.get(QUERY_BRANCH, params);
      this.branchList = get(branchRes, 'data', []);
      this.loading = false;
    },

    handleSelect(item) {
      // 选择地址
      const pages = getCurrentPages();
      const beforePage = pages[pages.length - 2];
      
      // 商品详情——地址选择
      if (
        beforePage && beforePage.route === 'pages/run/publish'
      ) {
        const options = {
          type: 'brachInfo',
          params: item,
        };
        uni.navigateBack({
          delta: 1,
          success: () => {
            // 触发上一页更新
            beforePage.onShow(options);
          },
        });
      }
    },
  },
};
</script>

<style lang="less">
.branch-list-wrap {
  position: relative;
  min-height: 100vh;
  padding-bottom: 300rpx;
  padding-bottom: calc(300rpx + constant(safe-area-inset-bottom));
  padding-bottom: calc(300rpx + env(safe-area-inset-bottom));
  background-color: #ffffff;
  box-sizing: content-box;
  .split-black {
    width: 100%;
    height: 20rpx;
    background-color: #f5f5f5;
  }

  .add {
    position: fixed;
    bottom: 0;
    left: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-direction: column;
    width: 100%;
    height: 108rpx;
    padding-top: 24rpx;
    padding-bottom: 28rpx;
    padding-bottom: calc(28rpx + constant(safe-area-inset-bottom));
    padding-bottom: calc(28rpx + env(safe-area-inset-bottom));
    box-sizing: content-box;
    &__btn {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 670rpx;
      height: 108rpx;
      font-size: 32rpx;
      font-weight: 500;
      border-radius: 54rpx;
      background-color: #fedd00;
      border: 1px solid #fedd00;
    }

    &__wx {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 670rpx;
      height: 108rpx;
      font-size: 32rpx;
      font-weight: 500;
      border-radius: 54rpx;
      background-color: #03be13;
      color: #ffffff;

      &--img {
        height: 68rpx;
        width: 68rpx;
        margin-right: 14rpx;
      }

      &--text {
      }
    }
  }

  .list {
    padding-left: 40rpx;

    &__li {
      width: 710rpx;
      position: relative;
      padding: 42rpx 30rpx 36rpx 0;
      border-bottom: 1px solid #eeeeee;

      &--title {
        display: flex;
        align-items: center;
        margin-bottom: 8rpx;

        &-name {
          font-size: 30rpx;
          font-weight: 500;
          line-height: 42rpx;
          margin-right: 52rpx;
        }

        &-mobile {
          font-size: 30rpx;
          font-weight: 500;
          line-height: 42rpx;
          margin-right: 18rpx;
        }

        &-default {
          height: 28rpx;
          padding: 0 6rpx;
          border-radius: 6rpx;
          font-size: 20rpx;
          color: #ffffff;
          background-color: #ff3841;
        }
      }

      &--address {
        max-width: 610rpx;
        font-size: 26rpx;
        line-height: 36rpx;
        color: #494949;
      }

      &--edit {
        position: absolute;
        top: 56rpx;
        right: 30rpx;
        width: 60rpx;
        height: 60rpx;
        padding: 20rpx;
      }
    }
  }
}
</style>
